<?php
/************************************
*   新浪相册 7.0
*   @file:function.php
*   @intro:常用函数
*   @author:shaopu@
*   @date:Sun Sep 20 17:33:27 CST 2009
************************************/

//各种ID的转换~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /**
     * 生成16进制UID
     *
     * @param int $uid
     * @return string
     */
    function uid2hex($uid)
    {
        return sprintf("%08x",$uid);
    }
    /**
     * 生成图片ID
     *
     * @param int $uid
     * @return string
     */
    function make_pic_id($uid)
    {
        return uid2hex($uid) . K_PIC_ID_SIGN . base_convert( uniqid() ,16,36);
    }
    /**
     * 根据资源号（图片ID，活动ID）取出UID
     *
     * @param string $res_id
     * @return int
     */
    function res2uid($res_id)
    {
        return hexdec(substr($res_id, 0, 8));
    }
    
//各种ID的检测~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
    /**
     * 检测用户 UID
     *
     * @param int $uid
     * @return bool
     */
    function is_uid($uid)
    {
        return is_numeric($uid);
    	//return (is_numeric($uid) && strlen($uid) <=10 && strlen($uid)>=5 );
    }
    
    function is_product_sn($sn)
    {
        return preg_match('/^[0-9a-zA-Z\-]{3,15}$/' , $sn);
    }
//各种地址的生成~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function img2dir($product_id , $http = false)
    {
        return ($http ? K5_HTTP_UPLOAD_PATH : K5_UPLOAD_PATH) . '/__product_images/d_'.($product_id % 16).'/';
    }
    
    function pid2path($product_id , $pid , $http = false)
    {
        return img2dir($product_id , $http).$pid;
    }
    
    function product2coverpath($product_id , $http = false)
    {
        return $http 
               ? K5_HTTP_UPLOAD_PATH.'/product_cover/'.'product_'.$product_id
               : K5_UPLOAD_PATH.'/product_cover/'.'product_'.$product_id;
    }
    function product2bigcoverpath($product_id , $http = false)
    {
        return $http 
               ? K5_HTTP_UPLOAD_PATH.'/product_cover/'.'big_product_'.$product_id
               : K5_UPLOAD_PATH.'/product_cover/'.'big_product_'.$product_id;
    }
    
    function url_sn2img($product_id , $pid)
    {
        return '/product/img/' . $product_id . '/' . $pid;
    }
    
//其他~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /**
     * 格式化标签
     *
     * @param string $tag
     * @return array
     */
    function tag_format($tag)
    {
    	$output = array();
    	$replace = array("，", " ", "　", ";", "；");
    	$tag = str_replace($replace, ",",$tag);
    	$arr = explode(",", $tag);
    	for($i = 0; $i < count($arr); $i++)
    	{
    		$arr[$i] = trim($arr[$i], "\x00..\x1F");
//    		$arr[$i] = trim($arr[$i], "\x80..\xA0");//某些中文会以这些字符结尾。所以不能做过滤。
    		if('' == trim($arr[$i])) continue;
    		$output[] = K_str::substr($arr[$i], 0, 30, '');//长度
    	}
    	$output = array_unique($output);
    	return $output;
    }

    function get_page_10($total , $numperpage , $curr_page , $format)
    {
    	$max_page = ceil($total / $numperpage);
    	
    	$html = '';
    	
    	parse_str($_SERVER['QUERY_STRING'] , $aQuery);
    	unset($aQuery['p']);
    	$url = '?'.http_build_query($aQuery);
    	
    	if($curr_page > 10)
    	{
    		$url_page = $format ? str_replace('{page}' , 1 , $format) : $url.'&p=1';
    		$html .= '<a href="'.$url_page.'">1</a> ... ';
    	}
    	
    	if($max_page <= 10)
    	{
    		for ($i = 1; $i <= $max_page ; $i++)
    		{
    			$url_page = $format ? str_replace('{page}' , 1 , $format) : $url.'&p=1';
    			
    			if($i == $curr_page)
    				$html .= ' <b>'.$i.'</b> ';
    			else 
    			{
    				$url_page = $format ? str_replace('{page}' , $i , $format) : $url.'&p='.$i;
    				$html .= ' <a href="'.$url_page.'">'.$i.'</a> ';
    			}
    		}
    	}
    	else 
    	{
    		$cut_num = ceil($curr_page / 10);
    		$curr_cut = floor($curr_page / 10);
    		$start = $curr_cut*10+1;
    		$end = $curr_cut*10 +10;
    		
    		$end = $max_page > $end ? $end : $max_page;

    		
    		if($start > 10)
    		{
    			$url_page = $format ? str_replace('{page}' , $start-10 , $format) : $url.'&p='.($start-10);
    			$html .= ' <a href="'.$url_page.'">&lt;&lt;</a> ... ';
    		}
    		
    		for ($i = $start; $i <= $end ; $i++)
    		{
    			$url_page = $format ? str_replace('{page}' , $i , $format) : $url.'&p='.$i;
    			
    			if($i == $curr_page)
    				$html .= ' <b>'.$i.'</b> ';
    			else 
    			{
    				$url_page = $format ? str_replace('{page}' , $i , $format) : $url.'&p='.$i;
    				$html .= ' <a href="'.$url_page.'">'.$i.'</a> ';
    			}
    		}
    		
    		if($curr_cut < $cut_num)
    		{
    			$url_page = $format ? str_replace('{page}' , $start+10 , $format) : $url.'&p='.($start+10);
    			$html .= ' ...<a href="'.$url_page.'">&gt;&gt;</a> ';
    		}
    	}
    	
    	
    	echo $html;
    }
    
    function get_page($total, $numperpage, $curr_page , $format = '')
    {
        if($total < $numperpage)
            return '';
        $sHtml = '';
    	$total_page = ceil( $total / $numperpage );
    	$curr_page = $curr_page > $total_page ? $total_page : $curr_page;
    	//
    	
    	parse_str($_SERVER['QUERY_STRING'] , $aQuery);
    	unset($aQuery['p']);
    	$url = '?'.http_build_query($aQuery);
    	
    	//从第几页开始
        $from = $curr_page - 6;
        $from = $from < 1 ? 1 : $from;
    
    	//到第几页结束
    	$to = $curr_page + 6;
    	$to = $to > $total_page ? $total_page : $to;
    	
    	   
    
    	//首页
    	if($total_page > 1 && $curr_page <> 1)
    	{
    	    $url_page = $format ? str_replace('{page}' , 1 , $format) : $url.'&p=1';
    	    $sHtml .= "<a href=\"".($url_page)."\">|&lt;</a>&nbsp;&nbsp;"; 
    	}
    
    	//本页之前的
    	for($i = $from ; $i < $curr_page ; $i++)
    	{
    	    $url_page = $format ? str_replace('{page}' , $i , $format) : $url.'&p='.$i;
    	   $sHtml .= "<a href=\"" . ($url_page). "\">" . $i ."</a>&nbsp;&nbsp;";
    	}
    	
    	//本页
    	$sHtml .= $curr_page.'&nbsp;&nbsp;';
    	
    	//本页之后的
    	for($i = $curr_page+1 ; $i <= $to ; $i++)
    	{
    	    $url_page = $format ? str_replace('{page}' , $i , $format) : $url.'&p='.$i;
    	   $sHtml .= "<a href=\"" . ($url_page) . "\">" . $i ."</a>&nbsp;&nbsp;";
    	}
    	
    	//尾页
    	if($total_page > 1 && $curr_page <> $total_page)
    	{
    	    $url_page = $format ? str_replace('{page}' , $total_page , $format) : $url.'&p='.$total_page;
    	    $sHtml .= "<a href=\"" . ($url_page). "\">&gt;|</a>";
    	}
    
    	return $sHtml;
    }
    /**
     * 输出含链接的标签字符串
     *
     * @param string $tag
     * @return string
     */
    function tag2link($tag)
    {
    	$output = array();
    	if (trim($tag) == '')
    	{
    		return $output;
    	}
    	$tag = strip_tags($tag);
    	$arr = explode(',', $tag);
    	foreach($arr as $tag)
    	{
    		//http://uni.sina.com.cn/c.php?t=album&k=绝色美女&ts=aphoto
    		$key = urlencode(K_str::utf2gb($tag));
    		$output[]="<a target=\"_blank\" href=\"http://uni.sina.com.cn/c.php?t=album&k={$key}&stype=tag\">{$tag}</a> ";
    	}
    	return $output;
    }
    /**
     * 格式化时间
     *
     * @param int $iTime
     * @param boolean $home : home页评论样式和其他页略有不同
     * @return string
     */
    function format_time($iTime , $home = false)
    {
        $iNow = time();
        $sReturn = '';
		$iTimeDiff = $iNow - $iTime;
		if ( $iTimeDiff < 3600) {
			$sReturn = ($iTimeDiff > 60) ? intval( $iTimeDiff / 60 ) : 1 ;
			$sReturn .= "分钟前";
		} elseif ($iTime < strtotime("yesterday")) {
	        if($home)
	        {
	            $sReturn = date("m-d H:i",$iTime);
	        }
	        else
	        {
	            $sReturn = date("Y-m-d H:i",$iTime);
	        }
		    
	    } elseif ($iTime < strtotime("today")) {
			$sReturn = "昨天&nbsp;".date("H:i",$iTime);
	    } else {
			$sReturn = "今天&nbsp;".date("H:i",$iTime);
		}
		return $sReturn;
    }
    
    /**
     * 格式化标签
     *
     * @param string $sTags
     * @return array
     */
    function format_tags($sTags)
	{
	   $sTags = str_replace(array(' ','　',';','；','，') , ',' , $sTags);
	   $aTags = explode(',' , $sTags);
	   if(count($aTags))
	   {
	       foreach ($aTags as $k => $tag)
	       {
	           $aTags[$k] = K_str::str_width($tag) > 8 ? K_str::substr($tag , 0 , 8) : $tag;
	       }
	   }
	   $aTags = array_slice($aTags , 0 , 5);
	   return $aTags;
	}
?>